<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class checklist extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct() {
		parent::__construct();
		ini_set('session.cookie_httponly',1); 
		ini_set('session.use_only_cookies',1);
		$this->load->model('checklist_class');
	}	 
	public function list_checklist(){
		$checklist_list = $this->checklist_class->list_checklist();
		//$data['qry']=$this->db->last_query();
		if($checklist_list!=0){
			foreach($checklist_list as $key=>$value){
				$data['intID'][$key]=$value['intID'];
				$data['strTitle'][$key]=$value['strTitle'];
				$data['strImgLink'][$key]=$value['strImgLink'];
			}
		}
		echo json_encode($data);
	}
	public function list_checklist_user(){
		$post = $this->input->post();
		$data=array();
		$a = @split('%3D', @base64_decode($post['session_code']));
		if($a[0]=='user_id'){
			$post['intUserID'] = $a['1'];
		}		
		$checklist_user = $this->checklist_class->get_checklist_user($post);
		//$data['test']=$post;
		//$data['qry']=$this->db->last_query();
		if($checklist_user==0){
			$checklist_preload = $this->checklist_class->list_checklist_preload($post);
			//$data['qry']=$this->db->last_query();
			if($checklist_preload!=0){
				foreach($checklist_preload as $key=>$value){
					$post['strDescription'] = $value['strDescription'];
					$post['intStatus'] = 0;
					$checklist_preload = $this->checklist_class->insert_checklist_user($post);
				}
			}
		}
		$list_checklist_user = $this->checklist_class->list_checklist_user($post);
		//$data['qry']=$this->db->last_query();
		if($list_checklist_user!=0){
			foreach($list_checklist_user as $key=>$value){
				$data['intID'][$key]= $value['intID'];
				$data['intCheckListID'][$key]= $value['intCheckListID'];
				$data['strDescription'][$key]= $value['strDescription'];
				$data['intStatus'][$key]= $value['intStatus'];
			}
		}
		echo json_encode($data);
	}
	public function update_intstatus_checklist(){
		$post = $this->input->post();
		$message=array();
		$a = @split('%3D', @base64_decode($post['session_code']));
		if($a[0]=='user_id'){
			$post['intUserID'] = $a['1'];
		}		
		$get_checklist_user = $this->checklist_class->get_checklist_user($post);
		//$data['qry']=$this->db->last_query();
		if($get_checklist_user!=0){
			$message = $this->checklist_class->update_intstatus_checklist($post);
		}
		echo json_encode($message);		
	}
	public function get_checklist_user(){
		$post = $this->input->post();
		$get_checklist_user = $this->checklist_class->get_checklist_user($post);
		echo json_encode($get_checklist_user);
	}
	public function save_checklist_user(){
		$post = $this->input->post();
		$a = @split('%3D', @base64_decode($post['session_code']));
		if($a[0]=='user_id'){
			$post['intUserID'] = $a['1'];
		}
		$get_checklist_user = $this->checklist_class->get_checklist_user($post);
		//$data['qry']=$this->db->last_query();
		//$data['test'] = $post['session_code'];
		$message=array();
		if($get_checklist_user!=0){
			$message = $this->checklist_class->update_checklist_user($post);
		} else {
			$post['intStatus'] = 0;
			$message = $this->checklist_class->insert_checklist_user($post);
		}
		echo json_encode($message);
	}
	public function save_checklist(){
		$post = $this->input->post();
		$message = '';
		$checklist = $this->checklist_class->get_checklist($post);
		if($checklist!=0){
			$message = $this->checklist_class->update($post);
		} else {
			$message = $this->checklist_class->insert($post);
		}
		echo json_encode($message);
	}	
	public function get_list_checklist(){
		$data=array();
	
		$checklist = $this->checklist_class->list_checklist();
		if($checklist!=0){
			foreach($checklist as $key=>$value){
				$data['intID'][$key]=$value['intID'];
				$data['strTitle'][$key]=$value['strTitle'];
				$data['strImgLink'][$key]=$value['strImgLink'];
				$data['intOrder'][$key]=$value['intOrder'];
			}
		}
		echo json_encode($data);
	}	
	public function get_checklist(){
		$post = $this->input->post();
		$checklist = $this->checklist_class->get_checklist($post);
		echo json_encode($checklist);
	}
	public function delete_checklist(){
		$post = $this->input->post();
		$checklist = $this->checklist_class->delete($post);
		echo json_encode($checklist);
	}	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */